package cn.tb.ymcc.web.controller;

import cn.tb.ymcc.domain.PayOrder;
import cn.tb.ymcc.dto.AlipayNotifyDto;
import cn.tb.ymcc.dto.PayApplyDto;
import cn.tb.ymcc.result.JSONResult;
import cn.tb.ymcc.service.IPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private IPayOrderService payOrderService;

    /**
     * 根据orderNo检查支付单是否生成
     * @return
     */
    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo){
        PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
        return payOrder != null?JSONResult.success():JSONResult.error();
    }

    /**
     * 发起支付申请
     * @param dto
     * @return
     */
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto dto){
        String form = payOrderService.apply(dto);
        return JSONResult.success(form);
    }

    /**
     * 支付宝异步回调接口
     * @param dto
     * @return
     */
    @PostMapping("/alipay/notify")
    public String alipayNotify(AlipayNotifyDto dto){
        try {
            return payOrderService.alipayNotify(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return "Fail";
        }
    }
}
